package controllers

import (
	"fmt"
	"net/url"
	"strconv"
	"strings"
	"time"

	"github.com/neo4j/neo4j-go-driver/v4/neo4j"
)

func (c *PageController) Get() { //changePage ?p=
	page := c.GetString("p")
	task_type := c.GetString("type")
	encode_user := c.Ctx.Input.Param(":user")
	encode_name := c.Ctx.Input.Param(":name")
	user, _ := url.QueryUnescape(encode_user)
	name, _ := url.QueryUnescape(encode_name)

	if page == "" {
		if task_type == "1" {
			c.TplName = "analysis.html"
		} else if task_type == "2" {
			c.TplName = "case.html"
		} else if task_type == "3" {
			c.TplName = "report.html"
		}
		return
	}

	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()

	result, _ := session.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) {
		p, _ := strconv.Atoi(page)
		if task_type == "1" { //竞品分析
			r, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}),
			(t)-[i:Include]->(p:Page{order:$order}) 
			return p.title as title, 
			p.targetid as target, 
			p.problem as problem, 
			p.advice as advice`, map[string]interface{}{"user": user, "name": name, "order": p})
			if err != nil {
				panic(err)
			}
			var obj Page
			if r.Next() {
				record := r.Record()
				title, _ := record.Get("title")
				target, _ := record.Get("target")
				problem, _ := record.Get("problem")
				advice, _ := record.Get("advice")
				obj.Title, _ = title.(string)
				obj.Target, _ = target.(string)
				obj.Problem, _ = problem.(string)
				obj.Advice, _ = advice.(string)
			}

			r, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}),
			(t)-[i:Include]->(p:Page{order:$order}),
			(p)-[c:Contains]->(pic:Pic)
			return c.order as order,c.title as title, pic.path as path order by order`, map[string]interface{}{"user": user, "name": name, "order": p})
			if err != nil {
				panic(err)
			}
			for r.Next() {
				record := r.Record()
				path, _ := record.Get("path")
				pathstr, _ := path.(string)
				order, _ := record.Get("order")
				orderint, _ := order.(int64)
				title, _ := record.Get("title")
				titlestr, _ := title.(string)
				obj.Pics = append(obj.Pics, Pic{Path: pathstr, Order: orderint, Title: titlestr})
			}

			return obj, nil
		} else if task_type == "2" { //特色化案例库
			r, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}),
			(t)-[i:Include]->(p:Page{order:$order}) 
			return 
			p.class as caseclass,
			p.title as title,
			p.case_num as case_num,
			p.product_class as product_class,
			p.name as name,
			p.version as version,
			p.app_class as app_class,
			p.date as mdate,
			p.feature as feature,
			p.abstract as abstract,
			p.page_num as page_num,
			p.detail as detail,
			p.idea as idea,
			p.recorder as recorder,
			p.checkList as checkList,
			p.reason as reason,
			p.username as username`, map[string]interface{}{"user": user, "name": name, "order": p})
			if err != nil {
				panic(err)
			}
			var obj PageCase
			if r.Next() {
				record := r.Record()
				caseclass, _ := record.Get("caseclass")
				title, _ := record.Get("title")
				case_num, _ := record.Get("case_num")
				product_class, _ := record.Get("product_class")
				name, _ := record.Get("name")
				version, _ := record.Get("version")
				app_class, _ := record.Get("app_class")
				date, _ := record.Get("mdate")
				feature, _ := record.Get("feature")
				abstract, _ := record.Get("abstract")
				page_num, _ := record.Get("page_num")
				detail, _ := record.Get("detail")
				idea, _ := record.Get("idea")
				recorder, _ := record.Get("recorder")
				checkList, _ := record.Get("checkList")
				reason, _ := record.Get("reason")
				username, _ := record.Get("username")
				obj.CaseClass, _ = caseclass.(string)
				obj.Title, _ = title.(string)
				obj.Case_num, _ = case_num.(string)
				obj.Product_class, _ = product_class.(string)
				obj.Name, _ = name.(string)
				obj.Version, _ = version.(string)
				obj.App_class, _ = app_class.(string)
				obj.Date, _ = date.(string)
				obj.Feature, _ = feature.(string)
				obj.Abstract, _ = abstract.(string)
				obj.Page_num, _ = page_num.(string)
				obj.Detail, _ = detail.(string)
				obj.Idea, _ = idea.(string)
				obj.Recorder, _ = recorder.(string)
				obj.Username = username.(string)
				if checkList == nil {
					obj.CheckList = ""
				} else {
					obj.CheckList = checkList.(string)
				}
				if reason == nil {
					obj.Reason = ""
				} else {
					obj.Reason = reason.(string)

				}
			}

			r, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}),
		(t)-[i:Include]->(p:Page{order:$order}),
		(p)-[c:Contains]->(pic:Pic) 
		return c.order as order, c.title as title, pic.path as path order by order`, map[string]interface{}{"user": user, "name": name, "order": p})
			if err != nil {
				panic(err)
			}
			for r.Next() {
				record := r.Record()
				path, _ := record.Get("path")
				order, _ := record.Get("order")
				orderint, _ := order.(int64)
				pathstr, _ := path.(string)
				title, _ := record.Get("title")
				titlestr, _ := title.(string)
				obj.Pics = append(obj.Pics, Pic{Path: pathstr, Order: orderint, Title: titlestr})
			}

			return obj, nil

		} else if task_type == "3" { //评测报告

			r, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}),
			(t)-[i:Include]->(p:Page{order:$order})    
			return p.title as title`,
				map[string]interface{}{"user": user, "name": name, "order": p})
			if err != nil {
				panic(err)
			}

			var page PageCheck

			if r.Next() {
				record := r.Record()
				title, _ := record.Get("title")
				page.Title = title.(string)
			}

			r, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}),
			(t)-[i:Include]->(p:Page{order:$order}),(p)-[:Inc]->(col:Col) return col.title as title,col.col as col order by col`,
				map[string]interface{}{"user": user, "name": name, "order": p})
			if err != nil {
				panic(err)
			}

			for r.Next() {
				var col Col
				record := r.Record()
				column, _ := record.Get("col")
				title, _ := record.Get("title")
				col.Title = title.(string)
				r, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}),
				(t)-[i:Include]->(p:Page{order:$order}),(p)-[:Inc]->(col:Col{col:$col}),(col)-[c:Contains]->(pic:Pic) 
				return pic.path as path order by c.order`,
					map[string]interface{}{"user": user, "name": name, "order": p, "col": column.(int64)})
				if err != nil {
					panic(err)
				}
				for r.Next() {
					record := r.Record()
					path := record.GetByIndex(0)
					col.Imgs = append(col.Imgs, path.(string))
				}
				page.Cols = append(page.Cols, col)
			}
			return page, nil
		} else {
			return nil, nil
		}

	})
	c.Data["json"] = result
	c.ServeJSON()
}

func (c *PageController) Get_pages() {
	encode_user := c.Ctx.Input.Param(":user")
	encode_name := c.Ctx.Input.Param(":name")
	user, _ := url.QueryUnescape(encode_user)
	name, _ := url.QueryUnescape(encode_name)
	t := c.GetString("type")

	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()
	result, _ := session.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) {
		var r neo4j.Result
		var err error
		var pages []PageOutline

		if t == "2" {
			r, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}), 
			(t)-[i:Include]->(p:Page) return p.case_num as c, p.order as o, p.date as d order by o`,
				map[string]interface{}{"name": name, "user": user})
			for r.Next() {
				results := r.Record()
				case_num, _ := results.Get("c")
				date, _ := results.Get("d")
				order, _ := results.Get("o")
				case_num_str, _ := case_num.(string)
				date_str, _ := date.(string)
				title := case_num_str + " " + date_str
				pages = append(pages, PageOutline{title, order.(int64)})
			}
		} else {
			r, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}), 
			(t)-[i:Include]->(p:Page) return p.title as t, p.order as o order by o`,
				map[string]interface{}{"name": name, "user": user})
			for r.Next() {
				results := r.Record()
				title, _ := results.Get("t")
				order, _ := results.Get("o")
				pages = append(pages, PageOutline{title.(string), order.(int64)})
			}
		}
		if err != nil {
			panic(err)
		}

		return pages, err
	})
	c.Data["json"] = result
	c.ServeJSON()
}

func (c *PageController) Addtoend() {
	encode_user := c.Ctx.Input.Param(":user")
	encode_name := c.Ctx.Input.Param(":name")
	user, _ := url.QueryUnescape(encode_user)
	name, _ := url.QueryUnescape(encode_name)
	t := c.GetString("type")
	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()
	if t == "1" {
		_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
			_, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}) 
			create (t)-[i:Include]->(p:Page{title:'',order:t.totalnum+1}) set t.totalnum=t.totalnum+1`,
				map[string]interface{}{"name": name, "user": user})
			if err != nil {
				panic(err)
			}

			return nil, nil
		})
	} else if t == "2" {
		d := time.Now()
		s := d.Format("2006-01-02")
		_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
			_, err := tx.Run(`match (n:User{username:$username})-[r:Control]->(t:Task{name:$name}) 
			create (t)-[i:Include]->(p:Page{order:t.totalnum+1, title:'',case_num:'',product_class:'',name:'',version:'',app_class:'', 
			date:$d, feature:'', abstract:'', page_num:'', detail:'', idea:'', username:$username}) set t.totalnum=t.totalnum+1`,
				map[string]interface{}{"username": user, "name": name, "d": s})
			if err != nil {
				panic(err)
			}

			return nil, nil
		})
	} else if t == "3" {
		_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
			_, err := tx.Run(`match (n:User{username:$username})-[r:Control]->(t:Task{name:$name}) 
			create (t)-[i:Include]->(p:Page{order:t.totalnum+1, title:''}) set t.totalnum=t.totalnum+1`,
				map[string]interface{}{"username": user, "name": name})
			if err != nil {
				panic(err)
			}

			return nil, nil
		})
	}
	c.Ctx.WriteString("1")
}

func (c *PageController) Addtonext() {
	encode_user := c.Ctx.Input.Param(":user")
	encode_name := c.Ctx.Input.Param(":name")
	user, _ := url.QueryUnescape(encode_user)
	name, _ := url.QueryUnescape(encode_name)
	case_num := c.GetString("case_num")
	page_num := c.GetString("page_num")
	feature := c.GetString("feature")
	p := c.GetString("p")
	t := c.GetString("type")
	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()
	pint, _ := strconv.Atoi(p)
	_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
		//后面的页数加一
		_, err := tx.Run("match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}), (t)-[i:Include]->(p:Page) where p.order>$order set p.order=p.order+1",
			map[string]interface{}{"name": name, "user": user, "order": pint})
		if err != nil {
			panic(err)
		}
		//总页数加一
		_, _ = tx.Run("match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}) set t.totalnum=t.totalnum+1",
			map[string]interface{}{"name": name, "user": user})

		if t == "1" {
			_, err = tx.Run("match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}) create (t)-[i:Include]->(p:Page{title:'', order:$order})",
				map[string]interface{}{"user": user, "name": name, "order": pint + 1})

		} else {
			caseclass := case_num[:1]

			/*顺序靠后的选出来 加一*/
			r, _ := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),(t)-[i:Include]->(p{class:$class}) where p.order>=$order 
				return p.case_num as case_num, p.page_num as page_num, p.order as order`,
				map[string]interface{}{"user": user, "task": name, "order": pint + 2, "class": caseclass})
			for r.Next() {
				record := r.Record()
				order_int, _ := record.Get("order")
				case_num_int, _ := record.Get("case_num")
				page_num_int, _ := record.Get("page_num")
				order, _ := order_int.(int64)
				case_num, _ := case_num_int.(string)
				num, _ := strconv.Atoi(case_num[1:])
				num += 1
				numstr := strconv.Itoa(num)
				for len(numstr) < 5 { //前面补0 5位
					numstr = "0" + numstr
				}
				numstr = caseclass + numstr

				page_num, _ := page_num_int.(string)
				page_nums := strings.Split(page_num, "/")
				currentstr := page_nums[0]
				totalstr := page_nums[1]
				current, _ := strconv.Atoi(currentstr)
				total, _ := strconv.Atoi(totalstr)
				current += 1
				total += 1
				newPageNum := strconv.Itoa(current) + "/" + strconv.Itoa(total)
				_, e := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),(t)-[i:Include]->(p{order:$order}) 
					set p.case_num=$case_num, p.page_num=$page_num`,
					map[string]interface{}{"user": user, "task": name, "order": order, "case_num": numstr, "page_num": newPageNum})
				if e != nil {
					panic(e)
				}
			}

			/*顺序靠前的选出来 total加一*/
			r, _ = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),(t)-[i:Include]->(p{class:$class}) where p.order<=$order 
				return p.case_num as case_num, p.page_num as page_num, p.order as order`,
				map[string]interface{}{"user": user, "task": name, "order": pint, "class": caseclass})
			for r.Next() {
				record := r.Record()
				order_int, _ := record.Get("order")
				page_num_int, _ := record.Get("page_num")
				order, _ := order_int.(int64)

				page_num, _ := page_num_int.(string)
				page_nums := strings.Split(page_num, "/")
				totalstr := page_nums[1]
				total, _ := strconv.Atoi(totalstr)
				total += 1
				newPageNum := page_nums[0] + "/" + strconv.Itoa(total)
				_, e := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),(t)-[i:Include]->(p{order:$order}) 
					set p.page_num=$page_num`,
					map[string]interface{}{"user": user, "task": name, "order": order, "page_num": newPageNum})
				if e != nil {
					panic(e)
				}
			}

			s := time.Now().Format("2006-01-02")
			num, _ := strconv.Atoi(case_num[1:])
			num += 1
			numstr := strconv.Itoa(num)
			for len(numstr) < 5 { //前面补0 5位
				numstr = "0" + numstr
			}
			numstr = caseclass + numstr

			page_nums := strings.Split(page_num, "/")
			currentstr := page_nums[0]
			totalstr := page_nums[1]
			current, _ := strconv.Atoi(currentstr)
			total, _ := strconv.Atoi(totalstr)
			current += 1
			total += 1
			newPageNum := strconv.Itoa(current) + "/" + strconv.Itoa(total)

			fmt.Println(feature)

			_, err = tx.Run(`match (n:User{username:$username})-[r:Control]->(t:Task{name:$name}) 
			create (t)-[i:Include]->(p:Page{order:$order, title:'',case_num:$case_num,product_class:'',name:'',version:'',app_class:'', 
			date:$d, feature:$feature, abstract:'', page_num:$page_num, detail:'', idea:'', username:$username, class:$class})`,
				map[string]interface{}{"username": user, "name": name, "d": s, "order": pint + 1, "case_num": numstr, "page_num": newPageNum, "feature": feature, "class": caseclass})

		}
		if err != nil {
			panic(err)
		}
		return nil, err
	})
	c.Ctx.WriteString("1")
}

func (c *PageController) DeletePage() {
	p := c.GetString("p")
	case_num := c.GetString("case_num")

	var caseClass string
	encode_user := c.Ctx.Input.Param(":user")
	encode_name := c.Ctx.Input.Param(":name")
	user, _ := url.QueryUnescape(encode_user)
	name, _ := url.QueryUnescape(encode_name)
	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()
	pint, _ := strconv.Atoi(p)

	_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {

		_, err := tx.Run(`match (u:User{username:$user})-[c:Control]->(t:Task{name:$name}),(t)-[i:Include]->(p:Page{order:$order}) detach delete p`, //删除本页
			map[string]interface{}{"user": user, "name": name, "order": pint})
		if err != nil {
			panic(err)
		}

		if case_num != "" {
			caseClass = case_num[0:1]

			//修改同类型后面的任务编号和页码编号
			r, err := tx.Run(`match (u:User{username:$user})-[c:Control]->(t:Task{name:$name}),(t)-[i:Include]->(p:Page{class:$class}) 
			where p.order>$order return p.page_num as page_num, p.order as order`, map[string]interface{}{"user": user, "name": name, "order": pint, "class": caseClass})
			if err != nil {
				panic(err)
			}
			for r.Next() {
				record := r.Record()
				page_num := record.GetByIndex(0)
				order := record.GetByIndex(1)
				order_int, _ := order.(int64)
				page_num_str, _ := page_num.(string)
				page_nums := strings.Split(page_num_str, "/")
				current, _ := strconv.Atoi(page_nums[0])
				total, _ := strconv.Atoi(page_nums[1])
				current -= 1
				total -= 1
				currentstr, totalstr := strconv.Itoa(current), strconv.Itoa(total)
				newCaseNum := currentstr
				for len(newCaseNum) < 5 {
					newCaseNum = "0" + newCaseNum
				}
				newCaseNum = caseClass + newCaseNum
				newPageNum := currentstr + "/" + totalstr
				_, e := tx.Run(`match (u:User{username:$user})-[c:Control]->(t:Task{name:$name}),(t)-[i:Include]->(p:Page{order:$order}) 
			set p.case_num=$new_case_num, p.page_num=$new_page_num`,
					map[string]interface{}{"user": user, "name": name, "order": order_int, "new_case_num": newCaseNum, "new_page_num": newPageNum})
				if e != nil {
					panic(e)
				}
			}

			//修改同类型前面的页码编号
			r, err = tx.Run(`match (u:User{username:$user})-[c:Control]->(t:Task{name:$name}),(t)-[i:Include]->(p:Page{class:$class})
				where p.order<$order return p.page_num as page_num, p.order as order`,
				map[string]interface{}{"user": user, "name": name, "order": pint, "class": caseClass})
			if err != nil {
				panic(err)
			}
			for r.Next() {
				record := r.Record()
				page_num := record.GetByIndex(0)
				order := record.GetByIndex(1)
				order_int, _ := order.(int64)
				page_num_str, _ := page_num.(string)
				page_nums := strings.Split(page_num_str, "/")
				total, _ := strconv.Atoi(page_nums[1])
				total -= 1
				totalstr := strconv.Itoa(total)
				newPageNum := page_nums[0] + "/" + totalstr
				_, e := tx.Run(`match (u:User{username:$user})-[c:Control]->(t:Task{name:$name}),(t)-[i:Include]->(p:Page{order:$order}) set p.page_num=$new_page_num`,
					map[string]interface{}{"user": user, "name": name, "order": order_int, "new_page_num": newPageNum})
				if e != nil {
					panic(e)
				}
			}
		}

		//后面的页面页码减一
		_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}),
		(t)-[i:Include]->(p:Page)
		where p.order>$p
		set p.order=p.order-1`,
			map[string]interface{}{"name": name, "user": user, "p": pint})
		if err != nil {
			panic(err)
		}

		//总页面数量减一
		_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name})
		set t.totalnum=t.totalnum-1`,
			map[string]interface{}{"name": name, "user": user})
		if err != nil {
			panic(err)
		}
		return nil, nil
	})
	c.Ctx.WriteString("1")
}

func (c *PageController) Downloadppt() {
	encode_user := c.Ctx.Input.Param(":user")
	encode_name := c.Ctx.Input.Param(":name")
	user, _ := url.QueryUnescape(encode_user)
	task, _ := url.QueryUnescape(encode_name)
	pathppt := "user/" + user + "/" + task + ".pptx"
	c.Ctx.Output.Download(pathppt)
}

func (c *PageController) Downloadxls() {
	encode_user := c.Ctx.Input.Param(":user")
	encode_name := c.Ctx.Input.Param(":name")
	user, _ := url.QueryUnescape(encode_user)
	task, _ := url.QueryUnescape(encode_name)
	pathxls := "user/" + user + "/" + task + ".xls"
	c.Ctx.Output.Download(pathxls)
}

func (c *PageController) Autosave() {
	t := c.GetString("type")
	encode_user := c.Ctx.Input.Param(":user")
	encode_name := c.Ctx.Input.Param(":task")
	user, _ := url.QueryUnescape(encode_user)
	task, _ := url.QueryUnescape(encode_name)
	page := c.GetString("p")
	pint, _ := strconv.Atoi(page)

	title := c.GetString("title")
	imgtitle0 := c.GetString("imgtitle0")
	imgpath0 := c.GetString("imgpath0")
	imgtitle1 := c.GetString("imgtitle1")
	imgpath1 := c.GetString("imgpath1")
	imgtitle2 := c.GetString("imgtitle2")
	imgpath2 := c.GetString("imgpath2")
	imgtitle3 := c.GetString("imgtitle3")
	imgpath3 := c.GetString("imgpath3")
	imgtitle4 := c.GetString("imgtitle4")
	imgpath4 := c.GetString("imgpath4")

	now := time.Unix(time.Now().Unix(), 0)
	modified := now.Format("2006-01-02 15:04:05")
	modified_by_day := now.Format("2006-01-02")

	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()

	if t == "1" { //竞品分析
		problem := c.GetString("problem")
		advice := c.GetString("advice")
		targetid := c.GetString("targetid")

		_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
			_, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}),
			(t)-[i:Include]->(p:Page{order:$order}) 
			set p.title=$title,p.problem=$problem,p.advice=$advice,p.targetid=$targetid,p.date=$date,t.modified=$modified`,
				map[string]interface{}{"user": user, "name": task, "order": pint, "title": title, "problem": problem, "advice": advice, "targetid": targetid, "modified": modified, "date": modified_by_day})
			if err != nil {
				panic(err)
			}

			return nil, nil
		})
	} else if t == "2" { //特色化案例库
		caseclass := c.GetString("caseclass")
		case_num := c.GetString("case_num")
		product_class := c.GetString("product_class")
		name := c.GetString("name")
		version := c.GetString("version")
		app_class := c.GetString("app_class")
		feature := c.GetString("feature")
		abstract := c.GetString("abstract")
		page_num := c.GetString("page_num")
		detail := c.GetString("detail")
		idea := c.GetString("idea")
		recorder := c.GetString("recorder")
		checkList := c.GetString("checkList")
		reason := c.GetString("reason")

		m := map[string]interface{}{"user": user, "task": task, "order": pint, "caseclass": caseclass, "title": title,
			"case_num": case_num, "product_class": product_class,
			"name": name, "version": version, "app_class": app_class, "feature": feature, "abstract": abstract,
			"page_num": page_num, "detail": detail, "idea": idea, "recorder": recorder, "modified": modified, "checkList": checkList, "reason": reason, "date": modified_by_day}

		_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
			r, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
			(t)-[i:Include]->(p:Page{order:$order}) return p.class as class`, m)
			if err != nil {
				panic(err)
			}
			if r.Next() {
				record := r.Record()
				classint, _ := record.Get("class")
				class, _ := classint.(string)
				if class != caseclass { //如果案例类型发生变化
					if class != "" {
						/*把原类型的顺序靠后的选出来 减一*/
						r, _ := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
					(t)-[i:Include]->(p:Page{class:$caseclass}) where p.order>$order return p.case_num as case_num, p.page_num as page_num, p.order as order`,
							map[string]interface{}{"user": user, "task": task, "order": pint, "caseclass": class})
						for r.Next() {
							record := r.Record()
							case_num_int, _ := record.Get("case_num")
							case_num, _ := case_num_int.(string)
							class := case_num[0:1]
							num, _ := strconv.Atoi(case_num[1:])
							num -= 1
							numstr := strconv.Itoa(num)
							for len(numstr) < 5 { //前面补0 5位
								numstr = "0" + numstr
							}
							numstr = class + numstr

							page_num_int, _ := record.Get("page_num")
							page_num, _ := page_num_int.(string)
							page_nums := strings.Split(page_num, "/")
							currentstr := page_nums[0]
							totalstr := page_nums[1]
							current, _ := strconv.Atoi(currentstr)
							total, _ := strconv.Atoi(totalstr)
							current -= 1
							total -= 1
							newPageNum := strconv.Itoa(current) + "/" + strconv.Itoa(total)

							order_int, _ := record.Get("order")
							order, _ := order_int.(int64)

							_, e := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
						(t)-[i:Include]->(p:Page{order:$order}) set p.case_num=$case_num,p.page_num=$page_num`,
								map[string]interface{}{"user": user, "task": task, "order": order, "case_num": numstr, "page_num": newPageNum})
							if e != nil {
								panic(e)
							}
						}

						/*把原类型的顺序靠前的选出来 减一*/
						r, _ = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
										(t)-[i:Include]->(p:Page{class:$caseclass}) where p.order<$order return p.case_num as case_num, p.page_num as page_num, p.order as order`,
							map[string]interface{}{"user": user, "task": task, "order": pint, "caseclass": class})
						for r.Next() {
							record := r.Record()
							page_num_int, _ := record.Get("page_num")
							page_num, _ := page_num_int.(string)
							page_nums := strings.Split(page_num, "/")
							totalstr := page_nums[1]
							total, _ := strconv.Atoi(totalstr)
							total -= 1
							newPageNum := page_nums[0] + "/" + strconv.Itoa(total)

							order_int, _ := record.Get("order")
							order, _ := order_int.(int64)

							_, e := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
											(t)-[i:Include]->(p:Page{order:$order}) set p.page_num=$page_num`,
								map[string]interface{}{"user": user, "task": task, "order": order, "page_num": newPageNum})
							if e != nil {
								panic(e)
							}
						}
					}

					/*把新类型的顺序靠后的选出来 加一*/
					r, _ = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),(t)-[i:Include]->(p{class:$class}) where p.order>=$order 
					return p.case_num as case_num, p.page_num as page_num, p.order as order`,
						map[string]interface{}{"user": user, "task": task, "order": pint, "class": caseclass})
					for r.Next() {
						record := r.Record()
						order_int, _ := record.Get("order")
						case_num_int, _ := record.Get("case_num")
						page_num_int, _ := record.Get("page_num")
						order, _ := order_int.(int64)

						case_num, _ := case_num_int.(string)
						num, _ := strconv.Atoi(case_num[1:])
						num += 1
						numstr := strconv.Itoa(num)
						for len(numstr) < 5 { //前面补0 5位
							numstr = "0" + numstr
						}
						numstr = caseclass + numstr

						page_num, _ := page_num_int.(string)
						page_nums := strings.Split(page_num, "/")
						currentstr := page_nums[0]
						totalstr := page_nums[1]
						current, _ := strconv.Atoi(currentstr)
						total, _ := strconv.Atoi(totalstr)
						current += 1
						total += 1
						newPageNum := strconv.Itoa(current) + "/" + strconv.Itoa(total)
						_, e := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),(t)-[i:Include]->(p{order:$order}) 
						set p.case_num=$case_num, p.page_num=$page_num`,
							map[string]interface{}{"user": user, "task": task, "order": order, "case_num": numstr, "page_num": newPageNum})
						if e != nil {
							panic(e)
						}
					}

					/*把新类型的顺序靠前的选出来 total加一*/
					r, _ = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),(t)-[i:Include]->(p{class:$class}) where p.order<$order 
					return p.case_num as case_num, p.page_num as page_num, p.order as order`,
						map[string]interface{}{"user": user, "task": task, "order": pint, "class": caseclass})
					for r.Next() {
						record := r.Record()
						order_int, _ := record.Get("order")
						page_num_int, _ := record.Get("page_num")
						order, _ := order_int.(int64)

						page_num, _ := page_num_int.(string)
						page_nums := strings.Split(page_num, "/")
						totalstr := page_nums[1]
						total, _ := strconv.Atoi(totalstr)
						total += 1
						newPageNum := page_nums[0] + "/" + strconv.Itoa(total)
						_, e := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),(t)-[i:Include]->(p{order:$order}) 
						set p.page_num=$page_num`,
							map[string]interface{}{"user": user, "task": task, "order": order, "page_num": newPageNum})
						if e != nil {
							panic(e)
						}
					}
				}
			}

			_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
			(t)-[i:Include]->(p:Page{order:$order})
			set p.class=$caseclass,p.title=$title,p.case_num=$case_num,p.product_class=$product_class,
			p.name=$name,p.version=$version,
			p.app_class=$app_class, p.feature=$feature, p.abstract=$abstract,
			p.page_num=$page_num, p.detail=$detail, p.idea=$idea, p.recorder=$recorder, p.checkList=$checkList,p.reason=$reason,p.date=$date,t.modified=$modified`, m)
			if err != nil {
				panic(err)
			}

			return nil, nil
		})

	} else if t == "3" {
		dataToSave := c.GetString("data")
		col := c.GetString("col")
		colNum, _ := strconv.Atoi(col)
		if col == "" { //保存title
			_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
				_, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}),
				(t)-[i:Include]->(p:Page{order:$order}) 
				set p.title=$title`,
					map[string]interface{}{"user": user, "name": task, "order": pint, "title": dataToSave})
				if err != nil {
					panic(err)
				}
				return nil, nil
			})
		} else { //保存列title
			_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
				_, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$name}),
				(t)-[i:Include]->(p:Page{order:$order}),(p)-[:Inc]->(col:Col{col:$col}) set col.title=$title`,
					map[string]interface{}{"user": user, "name": task, "order": pint, "col": colNum, "title": dataToSave})
				if err != nil {
					panic(err)
				}
				return nil, nil
			})
		}
		fmt.Println(dataToSave, col)
	}
	_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {

		if imgpath0 != "" {
			imgpath0 = imgpath0[1:]
			r, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
		(t)-[i:Include]->(p:Page{order:$order}),
		(p)-[c:Contains{order:1}]->(pic:Pic) return pic as pic`, map[string]interface{}{"user": user, "task": task, "order": pint})
			if err != nil {
				panic(err)
			}
			if r.Next() { //对应位置上已经有了图片，解开关系
				_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}), 
			(p)-[c:Contains{order:1}]->(pic:Pic) delete c`, map[string]interface{}{"user": user, "task": task, "order": pint})
				if err != nil {
					panic(err)
				}
			}
			r, err = tx.Run(`match (p:Pic{path:$path}) return p as pic`, map[string]interface{}{"path": imgpath0})
			if err != nil {
				panic(err)
			}
			if r.Next() { //如果这张图片已经存在
				_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}), (pic:Pic{path:$path}) create (p)-[c:Contains{title:$title,order:1}]->(pic)`,
					map[string]interface{}{"user": user, "task": task, "order": pint, "path": imgpath0, "title": imgtitle0})
				if err != nil {
					panic(err)
				}
			}
		} else { //删除图片，解除关系
			_, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}),(p)-[c:Contains{order:1}]->(pic:Pic) delete c`,
				map[string]interface{}{"user": user, "task": task, "order": pint})
			if err != nil {
				panic(err)
			}
		}

		if imgpath1 != "" {
			imgpath1 = imgpath1[1:]
			r, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
		(t)-[i:Include]->(p:Page{order:$order}),
		(p)-[c:Contains{order:2}]->(pic:Pic) return pic as pic`, map[string]interface{}{"user": user, "task": task, "order": pint})
			if err != nil {
				panic(err)
			}
			if r.Next() { //对应位置上已经有了图片，解开关系
				_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}), 
			(p)-[c:Contains{order:2}]->(pic:Pic) delete c`, map[string]interface{}{"user": user, "task": task, "order": pint})
				if err != nil {
					panic(err)
				}
			}
			r, err = tx.Run(`match (p:Pic{path:$path}) return p as pic`, map[string]interface{}{"path": imgpath1})
			if err != nil {
				panic(err)
			}
			if r.Next() { //如果这张图片已经存在
				_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}), (pic:Pic{path:$path}) create (p)-[c:Contains{title:$title,order:2}]->(pic)`, map[string]interface{}{"user": user, "task": task, "order": pint, "path": imgpath1, "title": imgtitle1})
				if err != nil {
					panic(err)
				}
			}
		} else { //删除图片，解除关系
			_, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}),(p)-[c:Contains{order:2}]->(pic:Pic) delete c`,
				map[string]interface{}{"user": user, "task": task, "order": pint})
			if err != nil {
				panic(err)
			}
		}

		if imgpath2 != "" {
			imgpath2 = imgpath2[1:]
			r, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
		(t)-[i:Include]->(p:Page{order:$order}),
		(p)-[c:Contains{order:3}]->(pic:Pic) return pic as pic`, map[string]interface{}{"user": user, "task": task, "order": pint})
			if err != nil {
				panic(err)
			}
			if r.Next() { //对应位置上已经有了图片，解开关系
				_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}), 
			(p)-[c:Contains{order:3}]->(pic:Pic) delete c`, map[string]interface{}{"user": user, "task": task, "order": pint})
				if err != nil {
					panic(err)
				}
			}
			r, err = tx.Run(`match (p:Pic{path:$path}) return p as pic`, map[string]interface{}{"path": imgpath2})
			if err != nil {
				panic(err)
			}
			if r.Next() { //如果这张图片已经存在
				_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}), (pic:Pic{path:$path}) create (p)-[c:Contains{title:$title,order:3}]->(pic)`, map[string]interface{}{"user": user, "task": task, "order": pint, "path": imgpath2, "title": imgtitle2})
				if err != nil {
					panic(err)
				}
			}
		} else { //删除图片，解除关系
			_, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}),(p)-[c:Contains{order:3}]->(pic:Pic) delete c`,
				map[string]interface{}{"user": user, "task": task, "order": pint})
			if err != nil {
				panic(err)
			}
		}

		if imgpath3 != "" {
			imgpath3 = imgpath3[1:]
			r, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
		(t)-[i:Include]->(p:Page{order:$order}),
		(p)-[c:Contains{order:4}]->(pic:Pic) return pic as pic`, map[string]interface{}{"user": user, "task": task, "order": pint})
			if err != nil {
				panic(err)
			}
			if r.Next() { //对应位置上已经有了图片，解开关系
				_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}), 
			(p)-[c:Contains{order:4}]->(pic:Pic) delete c`, map[string]interface{}{"user": user, "task": task, "order": pint})
				if err != nil {
					panic(err)
				}
			}
			r, err = tx.Run(`match (p:Pic{path:$path}) return p as pic`, map[string]interface{}{"path": imgpath3})
			if err != nil {
				panic(err)
			}
			if r.Next() { //如果这张图片已经存在
				_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}), (pic:Pic{path:$path}) create (p)-[c:Contains{title:$title,order:4}]->(pic)`, map[string]interface{}{"user": user, "task": task, "order": pint, "path": imgpath3, "title": imgtitle3})
				if err != nil {
					panic(err)
				}
			}
		} else { //删除图片，解除关系
			_, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}),(p)-[c:Contains{order:4}]->(pic:Pic) delete c`,
				map[string]interface{}{"user": user, "task": task, "order": pint})
			if err != nil {
				panic(err)
			}
		}

		if imgpath4 != "" {
			imgpath4 = imgpath4[1:]
			r, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
		(t)-[i:Include]->(p:Page{order:$order}),
		(p)-[c:Contains{order:5}]->(pic:Pic) return pic as pic`, map[string]interface{}{"user": user, "task": task, "order": pint})
			if err != nil {
				panic(err)
			}
			if r.Next() { //对应位置上已经有了图片，解开关系
				_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}), 
			(p)-[c:Contains{order:5}]->(pic:Pic) delete c`, map[string]interface{}{"user": user, "task": task, "order": pint})
				if err != nil {
					panic(err)
				}
			}
			r, err = tx.Run(`match (p:Pic{path:$path}) return p as pic`, map[string]interface{}{"path": imgpath4})
			if err != nil {
				panic(err)
			}
			if r.Next() { //如果这张图片已经存在
				_, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}), (pic:Pic{path:$path}) create (p)-[c:Contains{title:$title,order:5}]->(pic)`, map[string]interface{}{"user": user, "task": task, "order": pint, "path": imgpath4, "title": imgtitle4})
				if err != nil {
					panic(err)
				}
			}
		} else { //删除图片，解除关系
			_, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}), 
			(t)-[i:Include]->(p:Page{order:$order}),(p)-[c:Contains{order:5}]->(pic:Pic) delete c`,
				map[string]interface{}{"user": user, "task": task, "order": pint})
			if err != nil {
				panic(err)
			}
		}
		return nil, nil
	})

	c.Ctx.WriteString("success")
}

func (c *PageController) CasePages() {
	encode_user := c.Ctx.Input.Param(":user")
	encode_name := c.Ctx.Input.Param(":name")
	user, _ := url.QueryUnescape(encode_user)
	task, _ := url.QueryUnescape(encode_name)
	class := c.GetString("class")
	current := c.GetString("current")
	currentint, _ := strconv.Atoi(current)

	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()

	results, _ := session.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) {
		r, err := tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
		(t)-[i:Include]->(p:Page{class:$class}) return count(p) as total`,
			map[string]interface{}{"user": user, "task": task, "class": class})
		if err != nil {
			panic(err)
		}

		var casePageNum CasePageNum
		if r.Next() {
			record := r.Record()
			t, _ := record.Get("total")
			casePageNum.Total, _ = t.(int64)
		}
		r, err = tx.Run(`match (u:User{username:$user})-[r:Control]->(t:Task{name:$task}),
		(t)-[i:Include]->(p:Page{class:$class}) where p.order<$current return count(p) as current`,
			map[string]interface{}{"user": user, "task": task, "class": class, "current": currentint})
		if err != nil {
			panic(err)
		}

		if r.Next() {
			record := r.Record()
			c, _ := record.Get("current")
			casePageNum.Current = c.(int64)
		}
		return casePageNum, nil
	})
	c.Data["json"] = results
	c.ServeJSON()
}

func (c *PageController) AddRmCol() {
	encode_user := c.Ctx.Input.Param(":user")
	encode_name := c.Ctx.Input.Param(":name")
	user, _ := url.QueryUnescape(encode_user)
	task, _ := url.QueryUnescape(encode_name)
	p, _ := strconv.Atoi(c.GetString("p"))
	col, _ := strconv.Atoi(c.GetString("col"))
	t := c.GetString("type")

	fmt.Println(user, task, p, col)

	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()
	_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {

		if t == "add" {
			_, err := tx.Run(`match (u:User{username:$user})-[:Control]->(t:Task{name:$task}),
		(t)-[:Include]->(p:Page{order:$order}) 
		create (p)-[i:Inc]->(col:Col{title:'',col:$col})`,
				map[string]interface{}{"user": user, "task": task, "order": p, "col": col})
			if err != nil {
				panic(err)
			}
		} else {
			_, err := tx.Run(`match (u:User{username:$user})-[:Control]->(t:Task{name:$task}),
			(t)-[:Include]->(p:Page{order:$order}),(p)-[:Inc]->(col:Col{col:$col}) detach delete col`,
				map[string]interface{}{"user": user, "task": task, "order": p, "col": col})
			if err != nil {
				panic(err)
			}
		}
		return nil, nil
	})

	c.Ctx.WriteString("")
}

func (c *PageController) RmImg() {
	encode_user := c.Ctx.Input.Param(":user")
	encode_name := c.Ctx.Input.Param(":name")
	user, _ := url.QueryUnescape(encode_user)
	task, _ := url.QueryUnescape(encode_name)
	p, _ := c.GetInt("p")
	col, _ := c.GetInt("col")
	index, _ := c.GetInt("index")
	driver, session := GetNeo4jConn()
	defer driver.Close()
	defer session.Close()
	_, _ = session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {

		_, err := tx.Run(`match (u:User{username:$user})-[:Control]->(t:Task{name:$task}),
		(t)-[:Include]->(p:Page{order:$order}),(p)-[:Inc]->(col:Col{col:$col}),
		(col)-[c:Contains{order:$rank}]->(pic:Pic) delete c`,
			map[string]interface{}{"user": user, "task": task, "order": p, "col": col, "rank": index + 1})

		if err != nil {
			panic(err)
		}

		/*将序号大于index+1的图片序号减一*/

		_, err = tx.Run(`match (u:User{username:$user})-[:Control]->(t:Task{name:$task}),
		(t)-[:Include]->(p:Page{order:$order}),(p)-[:Inc]->(col:Col{col:$col}),
		(col)-[c:Contains]->(pic:Pic) where c.order>$rank set c.order=c.order-1`,
			map[string]interface{}{"user": user, "task": task, "order": p, "col": col, "rank": index + 1})

		if err != nil {
			panic(err)
		}

		return nil, nil
	})

	c.Ctx.WriteString("")
}
